/**
 * @(#)Data.java
 *
 * Copyright: Copyright (c) 2003 Carnegie Mellon University
 *
 * This program is the implementation of the original system in the Assignment 2 of "Architectures
 * of Software Systems" class. For the details of each classes, please refer to the provided javadoc
 * documents. You will also find some useful information such as implementation decisions, coding
 * standards, and naming conventions in the README file.
 *
 * History:
 *
 * Date          Author              Change
 * ----------    ----------------    ---------------------------------------------------------------
 * 07/15/2003    Jung Soo Kim        Initial version.
 */


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.StringTokenizer;


/**
 * A data node provides access to student and course data including reading the record information
 * and writing registration information. Note that current version of data node doesn't support
 * writing student and course records to files. A shutdown means loss of all registration
 * information.
 */
public class Security extends UnicastRemoteObject implements RISecurity {

    private static final String DATA_NAME = "Security";

    /**
     * A list of <code>Student</code> objects containing student records.
     */
    protected ArrayList vStudent;

    
    public Security(String sStudentFileName)
           throws RemoteException, FileNotFoundException, IOException {
        // Open the given student and course files.
        BufferedReader objStudentFile = new BufferedReader(new FileReader(sStudentFileName));

        // Initialize student and course lists.
        this.vStudent = new ArrayList();

        // Populate student and course lists.
        while (objStudentFile.ready()) {
            this.vStudent.add(new SIDPwd(objStudentFile.readLine()));
        }
       

        // Close the student and course files.
        objStudentFile.close();
    }   


    /**
     * Return all student records as a list.
     *
     * @return an <code>ArrayList</code> of <code>Student</code> objects containing student records
     */
    public boolean authorizeUser(String sSID, String pwd)
    		throws RemoteException {
    	
    	for (int i=0; i<this.vStudent.size(); i++) {
    		SIDPwd objStudent = (SIDPwd) this.vStudent.get(i);
            if (objStudent.match(sSID)) 
            {
            	
            	boolean equal = objStudent.getPWD().equals(pwd);
                return equal;
            }
        }
        // Return the student list as it is.
        return false;
    }

   
    public static void main(String args[]) {
        // Check the number of parameters.
        if (args.length != 1) {
            System.err.println("Incorrect number of parameters");
            System.err.println("Usage: java Data <SecurityFile>");
            System.exit(1);
        }

        // Check if input files exists.
        if (!new File(args[0]).exists()) {
            System.err.println("Could not find " + args[0]);
            System.exit(1);
        }
        

        try {
            // Create a data node and make it available through RMI.
            Security objData = new Security(args[0]);
            Naming.bind(DATA_NAME, objData);
            System.out.println("Data node is ready to serve.");

            // Wait for user interrupt.
            System.out.println("Press Enter to terminate.");
            System.in.read();

            // Clean up and finish.
            Naming.unbind(DATA_NAME);
            System.out.println("Data node is leaving, bye. Press ctrl-c if the delay is too long.");
        }
        catch (java.rmi.ConnectException e) {
            // Show error message and quit.
            System.err.println("Java RMI error: check if rmiregistry is running.");
            System.exit(1);
        }
        catch (Exception e) {
            // Dump the exception information for debugging.
            System.err.println("Unexpected exception at " + DATA_NAME);
            e.printStackTrace();
            System.exit(1);
        }
    }
}
